//
//  ESSetViewController.m
//  Eyeshot
//
//  Created by admin on 15/7/1.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//
#import "ESOurVC.h"
#import "ESSetViewController.h"
@interface ESSetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) UITableView *setTableView;
@property (nonatomic,strong) NSMutableArray *dataSourceArr;
@property (nonatomic,strong) UISwitch *collSwitch;
@property (nonatomic,strong) UISwitch *nightSwitch;
@property (nonatomic,strong) UISegmentedControl *segControl;
@end

@implementation ESSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSetTableView];
    //把switch开关的状态先写入NSUserDefaults的plist文件内，页面加载后先读取开关状态
    self.collSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"collection"];
    self.nightSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"night"];
    self.segControl.selectedSegmentIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"model"];
    //打印出沙盒路径
//    NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
 
   
}

- (void)setupSetTableView
{
    
    UITableView *setTableView = [[UITableView alloc] initWithFrame:CGRectMake(ZERO, ZERO, SCREEN_WIDTH, SCREEN_HEIGHT - BAR_HEIGHT) style:(UITableViewStyleGrouped)];
    self.setTableView = setTableView;
    [self.setTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.setTableView.delegate = self;
    self.setTableView.dataSource = self;
    [self.view addSubview:self.setTableView];

}
#pragma mark - 懒加载
- (NSMutableArray *)dataSourceArr
{
    if (!_dataSourceArr) {
        self.dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}
-(UISwitch *)collSwitch
{
    if (!_collSwitch) {
        self.collSwitch = [[UISwitch alloc] init];
        [self.collSwitch addTarget:self action:@selector(changeCollection:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _collSwitch;
}

- (UISwitch *)nightSwitch
{
    if (!_nightSwitch) {
        self.nightSwitch = [[UISwitch alloc] init];
        [self.nightSwitch addTarget:self action:@selector(changeModel:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _nightSwitch;
}

- (UISegmentedControl *)segControl
{
    NSArray *itemArr = @[@"小",@"中",@"大"];
    if (!_segControl) {
        self.segControl = [[UISegmentedControl alloc] initWithItems:itemArr];
        [self.segControl addTarget:self action:@selector(changeSegmentIdenx:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _segControl;
}
#pragma mark - 按钮事件
- (void)changeCollection:(UISwitch *)swit
{
    if (swit.on) {
        //把对应的开关状态写入NSUserDefaults内
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"collection"];
    }else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"collection"];
    }
}

- (void)changeModel:(UISwitch *)swit
{
    if (swit.on) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"night"];
    }else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"night"];
    }
}

- (void)changeSegmentIdenx:(UISegmentedControl *)segControl
{
    switch (segControl.selectedSegmentIndex) {
        case 0:
        {
            [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"model"];
            
        }
            break;
        case 1:
        {
            
            [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"model"];
        }
            break;
        case 2:
        {
            
             [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"model"];
        }
            break;
        default:
            break;
    }
}

- (void)getDataSourceArr
{
    self.dataSourceArr = [@[@[@"字体大小",@"转发时收藏",@"摇一摇夜间模式"],@[@"清除缓存",@"推荐给好友",@"关于我们",@"当前版本:1.0",@"帮助"]] mutableCopy];
}

#pragma mark - UITableViewDelegate
//UITableViewStyleGrouped 类型的UITableView 用下面方法设置 区头，区尾
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 40;
    }
    return 0;
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        _setTableView.sectionHeaderHeight = 0;
        return 40;
    }
    return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section == 0) {
    _setTableView.sectionHeaderHeight = 0;
        return @"其他";
    
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
                [MBProgressHUD showSuccess:@"清除缓存成功"];
                break;
            case 1:
            {
              
            }
                break;
            case 2:
            {
                ESOurVC *ourVC = [[ESOurVC alloc] init];
               
                [self.navigationController pushViewController:ourVC animated:YES];
    
            }
                
                break;
            case 4:
                
                break;
            default:
                break;
        }
    }
}

#pragma mark - UITableViewDataSource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
      
        return @"功能设置";
    }
    return nil;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1)
    {
         return 5;
    }
    return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self getDataSourceArr];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
 
    cell.textLabel.text = self.dataSourceArr[indexPath.section][indexPath.row];
    if (indexPath.section == 0 && indexPath.row == 1) {
    
        cell.accessoryView = self.collSwitch;
    }else if (indexPath.section == 0 && indexPath.row == 2)
    {
        cell.accessoryView = self.nightSwitch;
    }else if (indexPath.section == 0 && indexPath.row == 0)
    {
        cell.accessoryView = self.segControl;
    }else if (indexPath.section == 1 && indexPath.row != 3)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.alpha = 0.8;
    return cell;
}





@end
